﻿using QuanLySieuThi.Controllers;
using QuanLySieuThi.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLySieuThi.Views
{
    public partial class frmNhaCungCap : Form
    {
        public frmNhaCungCap()
        {
            InitializeComponent();
        }

        private void frmNhaCungCap_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = QuanLyNCC.UniqueObject.GetDataTable;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >=0)
            {
                var r = dataGridView1.Rows[e.RowIndex];
                txtDiaChi.Text = r.Cells["DiaChi"].Value.ToString();
                txtMaNCC.Text = r.Cells["MaNhaCungCap"].Value.ToString();
                txtSDT.Text = r.Cells["SDT"].Value.ToString();
                txtTenNCC.Text = r.Cells["TenNhaCungCap"].Value.ToString();
            }
        }

        private void Them_Click(object sender, EventArgs e)
        {
            QuanLyNCC.UniqueObject.Add(getNCCfromView());
            dataGridView1.DataSource = QuanLyNCC.UniqueObject.GetDataTable;
            MessageBox.Show("OK");

        }

        private NhaCungCap getNCCfromView()
        {
            NhaCungCap ncc = new NhaCungCap();
            ncc.DiaChi = txtDiaChi.Text;
            ncc.MaNhaCungCap = txtMaNCC.Text;
            ncc.SDT = txtSDT.Text;
            ncc.TenNhaCungCap = txtTenNCC.Text;
            return ncc;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtDiaChi.Text = "";
            txtMaNCC.Text = "";
            txtSDT.Text = "";
            txtTenNCC.Text = "";
        }

        private void CapNhat_Click(object sender, EventArgs e)
        {
            QuanLyNCC.UniqueObject.Update(getNCCfromView());
            dataGridView1.DataSource = QuanLyNCC.UniqueObject.GetDataTable;
            MessageBox.Show("OK");
        }

        private void txtMaNCC_TextChanged(object sender, EventArgs e)
        {
           
        }
    }
}
